Thread: get rid of warning: the use of `tmpnam' is dangerous, better use `mkstemp'

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    23

    Question get rid of warning: the use of `tmpnam' is dangerous, better use `mkstemp'

    hi,

    i do know about the difference of tmpnam and mkstemp, but because i need
    to get a "free" name to open the filestream later by myself instead of the
    mkstemp that already opens the stream and creates the file as soon as it
    is called.
    is there a possibility to get rid of this warning, i mean, not on my computer
    but on computers of others who might compile the program?
    or is there a possibility to use a function like mkstemp but not create and
    open the file at the same time that does not give this warning?

    thanks,

    toby

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Use safe functions at the expensive of almost anything else.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    How are you intending to handle the possibility that between creating the name and using the name, that something else won't come along and use the same name?

    That is what mkstemp is designed to avoid, those kinds of race conditions.

    Besides, if you really need to know it's name, then perhaps mkstemp (and even tmpnam) is the wrong thing to be using. Perhaps roll your own unique name, and add in the current process ID. That stands a fair chance of being unique.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    If I understand correctly, the poster probably wants an fstream for the temp file, which poses an interesting problem indeed.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Your compiler probably has a #pragma to ignore certain warnings, but I agree with the others -- you should always try to use the most secure function available.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Secure versions are easy to get rid of if others want to compile your code with simple pseudo functions or defines, but it's not easy the other way around, so by all means - go for the safe functions!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM